Forth.Starting Forth
:
for new definitionsword
defined command- convention: separate name from contents by 3 spaces
CR
carriage return;EMIT
emit ascii char;SPACES
,SPACE
tsia0 DO ... LOOP
Compiling
translates definitions to dictionary form- Execution
- ANS Forth allows for 31 characters of a name to be stored
."
(dot-quote) for strings inside definitions
- numbers are stored on the stack
- postfix allows using all numbers from the stack
- Accessing the stack isn't scoped: words just work
.
removes and prints numbers from the stackStack underflow
on trying to pop too muchGlossary
list of newly created words / meanings- Stack notation
- Words are fully compiled and don't retain references
+
,-
,*
,/
- Operators are integers; single length signed numbers
- Can have no-op words as well eg
: INCH ;
/MOD
( n1 n2 – rem quot )MOD
( n1 n2 – rem )SWAP
switches top two stack itemsDUP
duplicates it,OVER
copies second item to topROT
rotates third item to top,DROP
drops top item.S
non destructive stack print2SWAP
,2DUP
,2OVER
,2DROP
- Text interpreter always starts at the back of the dictionary
FORGET
removes most recent definition of wordMARKER <label>
retains current system state
INCLUDE <filename>
to grab a file- dictionary entries go to memory, source text is saved to disk
USE <filename>
sets up a block( ... )
act as comments, everything between () is ignoredLIST
lists a disk block,LOAD
loads a disk block1 BLOCK
copies block 1 of open file into systemBlock
division of disk memory with up to 1024 charsBuffer
temporary storage area<CONDITION> IF <IF-TRUE> ELSE <IF-FALSE> THEN <ALWAYS EXECUTED>
- #+BEGINSRC
, <>, 0
, 0>, 0<, <, > #+ENDSRC INVERT
is the not used after conditionalsOR
,AND
?DUP
ABORT"
PAGE
clear terminal screenABS
absolute value,NEGATE
,MIN
,MAX
- Parameter Stack – standard stack used by parameters
- Return Stack --
>R
moves value from parameter to return stackR>
moves value from return to parameter stackR@
copies top item from return stack to parameter stack- Processors that support hardware floating point are faster than fixed point
- Scaling operators help with fixed point
*/
( n1 n2 n3 – n4 ) n1 * n2 / n3 with a double length intermediate result*/MOD
similar- Can approximate constants by pairs of integers with errors < 10-8
- Definite loop:
<LIMIT> <INDEX> DO...LOOP
U.R
unsigned number print, right justifiedDO ... <INC> +LOOP
uses a custom increment
- Adding random words triggers
ABORT
, which also clears all stacks - Indefinite loop:
BEGIN ... <flag> UNTIL
- Infinite loop:
BEGIN ... AGAIN
BEGIN ... <f> WHILE ... REPEAT
LEAVE
causes loops to immediately endQUIT
removes ok at end of executionNIP
drop first item below stack2*
arithmetic left shift,2/
arithmetic right shift- Stack holds unsigned or signed numbers
.,
prints stack value as signed numberU.
prints it as unsignedUM*
,UM/MOD
,U<
– single length unsigned opsHEX
,OCTAL
,DECIMAL
change base- Prefix hexadecimals with a 0
- = n BASE ! ;= Change base
- Number followed by a . is converted to a double cell number
D.
print double length number<# #S #> TYPE ;
HOLD
inserts character at current position<# ... #>
converts double length unsigned values to strings<# ... ROT SIGN #>
double length signed value etc. etc.M+
double length + single lengthSM/REM
d1 / n1, returning n2 and n3FM/MOD
,M*
,M*/
- Defined numbers get compiled into the dictionary, depending on defined base
VARIABLE DATE
, creating variables;
- Variables are useful for values that need to be changed after the definition has been compiled
+!
increment stored value- =<VALUE> CONSTANT <NAME> =
2VARIABLE
,2CONSTANT
,2!
,2@
VARIABLE <name> <x> CELLS ALLOT
FILL (addr n char -- )
fills n bytesERASE (addr n -- )
zeroes out n bytesaddr count DUMP
to print out arrays- Can also use byte arrays:
C! ( char addr - )
,C@ ( addr - )
CREATE name <value1> , <value2> , <value3> ... ,
'
Identifies location in memory from dictionary, returns execution tokenEXECUTE
executes token on stackDUMP
shows raw contents- Vectored execution
- Label pointers with a
'
prefix, eg.'aloha
- Tick always goes to the next word in the input stream, not the definition
[']
goes to the next word in the definition- Dictionary entries
- name field, link field, code/code pointer field and data field
- name: first byte defines number of characters, then an ascii rep of the name
- precedence bit: if it should be executed during compilation or compiled into the definition
- link: address of previous definition, allows for searching dictionary
- code pointer
- data field
- execution token is code pointer
- name field, link field, code/code pointer field and data field
EXIT
returns execution to next higher level definition, return from subroutine- return stack keeps return addresses
QUIT
is the outermost loop,INTERPRET
s and prints oks.- Memory map
- forth / system variables / load definitions / user dictionary (stack)
- pad stores ascii, etc. that are being manipulated
- parameter stack grows up
- TIB grows down / terminal input buffer – stores text from terminal
- return stack grows up
- user variables/buffers at the end
CP
points to the next cell in dictionaryHERE
putsCP
on the stack, can be used to estimate size of stackSP
provides address of top stack locationS0
contains address of cell below the empty stackTIB
,#TIB
,BASE
,H
,>IN
- User variables
- forth / system variables / load definitions / user dictionary (stack)
- Vocabularies manage the dictionary
EMIT
ASCII re on the stack and prints itTYPE
prints entire string given starting address in memory